qtre.helper = {

	createTraceCamera : function(name, renderToBuffer){
		//uniform properties
		var eye = new Float32Array([0, 0, 10]);
		var target = new Float32Array([0, 0, 0]);
		var up = new Float32Array([0, 1, 0]);
		/**********************************************************
		 * pass1: main scene
		 *********************************************************/
		var camera = qtre.sceneManager.createCamera(name, {
			eye : eye,
			target : target,
			up : up,
			fov : tdl.math.degToRad(45),
			near : 1,
			far : 5000
		})
		
		if(renderToBuffer){
			var fbo = tdl.framebuffers.createFramebuffer(1024, 1024, true);
			fbo.unbind();
			camera.addRenderBeginCallback(function(){
				fbo.bind();
			})
			camera.addRenderEndCallback(function(){
				fbo.unbind();
			})
		}

		var horizontalSceneNode = qtre.sceneManager.createSceneNode(name + "_camera_horizantal");
		horizontalSceneNode.attachObject(camera);
		var verticalSceneNode = qtre.sceneManager.createSceneNode(name + "_camera_vertical");
		verticalSceneNode.addChild(horizontalSceneNode);

		qtre.input.addEventListener("mousemove", function(){
			if(qtre.input.mouseDown){
				verticalSceneNode.rotateX(Math.PI/600*qtre.input.dy);
				horizontalSceneNode.rotateY(Math.PI/600*qtre.input.dx);
				//sceneNode.rotateY(Math.PI/360*qtre.input.dx);
			}
		})
		return {
			"camera" : camera,
			"fbo" : fbo
		}
	},

	createDepthPass : function(camera){
		var fbo = tdl.framebuffers.createFramebuffer(1024, 1024, true);
		var program = tdl.programs.loadProgram("uniform mat4 worldViewProjection;\n\
												attribute vec4 position;\n\
												varying vec4 v_position;\n\
												void main() {\n\
													v_position = (worldViewProjection * position);\n\
													gl_Position = v_position;\n\
												}",
												"#ifdef GL_ES\n\
												precision highp float;\n\
												#endif\n\
												varying vec4 v_position;\n\
												void main() {\n\
													float depth = v_position.z / v_position.w;\n\
													//vec4 shift = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);\n\
													gl_FragColor =  vec4(depth, depth, depth, 1.0);\n\
												}");

		var material = qtre.sceneManager.createMaterial(camera.name + "_depth", program);
		material.bindCamera(camera);

		camera.addRenderBeginCallback(function(){
			fbo.bind();
		})
		camera.addRenderEndCallback(function(){
			fbo.unbind();
		})

		return {
			"material" : material,
			"fbo" : fbo
		}
	}
}